package xyz.longicorn.driver.util;

import lombok.SneakyThrows;
import net.coobird.thumbnailator.Thumbnails;

import java.io.File;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

public class FileUtils {
    private static final List<String> ImageTypes = Arrays.asList("png", "jpg", "jpeg", "gif", "bmp", "webp");
    private static final List<String> DocumentsTypes = Arrays.asList("doc", "pdf", "docx", "ppt", "pptx", "xls", "xlsx");
    private static final List<String> VideoTypes = Arrays.asList("mp4", "mkv", "mpg");
    private static final List<String> AudioTypes = Arrays.asList("mp3", "wav", "ogg", "flac");

    /**
     * 根据类型判断是否是图片
     *
     * @param type
     * @return
     */
    public static boolean isImage(String type) {
        return ImageTypes.contains(type.toLowerCase());
    }
    public static boolean isDocument(String type) {
        return DocumentsTypes.contains(type.toLowerCase());
    }
    public static boolean isVideo(String type) {return VideoTypes.contains(type.toLowerCase());
    }
    public static boolean isAudio(String type) {
        return AudioTypes.contains(type.toLowerCase());
    }

    @SneakyThrows
    public static void getImagePreview(String imagePath, OutputStream os) {
        Thumbnails.of(new File(imagePath))
                .size(256, 256) // 尺寸 256x256
                .outputFormat("jpg") // 格式
                .toOutputStream(os);
    }
}
